package suanfa;

public class removeNthFromEnd_1 {
    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode now = head;
        while (now != null) {
            n--;
            now = now.next;
        }
        if (n>0)
            throw new IllegalArgumentException();
        if (n == 0)
            return head.next;
        ListNode top = head;
        if (n < 0) {
            while (n < 0) {
                head = head.next;
                n++;
            }

        }
        head.next = head.next.next;
        return top;
    }
}
